<?php

namespace App\Http\Controllers\Wechat\Shop;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\Api\Shop\OrderRequest;
use App\Repos\OrderRepo;

class OrderController extends Controller
{
    public function index(OrderRequest $request)
    {
    	$rs['status'] = false;
        $where['shop_id'] = $request->shopUser->shop_id;
        if($request->status) {
            $where['status']  = $request->status;    
        }
        
    	$list = OrderRepo::getPages($where);
        if($list) {
            $rs['status'] = true;
            $rs['data'] = $list;
            return response()->json($rs);
        }
    	return response()->json($rs);
    }

    public function info(OrderRequest $request)
    {
        $rs['status'] = false;
        $where['shop_id'] = $request->shopUser->shop_id;
        $where['id']  = $request->order_id;
        $data = OrderRepo::find($where);
        if($data) {
            $rs['status'] = true;
            $rs['data'] = $data;
            return response()->json($rs)->setEncodingOptions(JSON_NUMERIC_CHECK);
        }
        return response()->json($rs);
    }

    /**
     * 确认订单
     * @param order_id,shop_user_id
     */
    public function confirm(OrderRequest $request)
    {
        $rs['status'] = false;
        $data = $request->pure();
        $data['shop_user_id'] = $request->shopUser->id;
        $flag = OrderRepo::confirm($data);
        if($flag['status']==true) {
            $rs['status'] = true;
            return response()->json($rs);
        }
       
        $rs['msg'] = $flag['msg'];
        return response()->json($rs);
    }

    /**
     * 拒绝订单
     */
    public function reject(OrderRequest $request)
    {
        $rs['status'] = false;
        $data['id']      = $request->order_id;
        $data['shop_id'] = $request->shopUser->shop_id;
        $data['shop_user_id'] = $request->shopUser->id;
        $flag = OrderRepo::reject($data);
         if($flag['status']==true) {
            $rs['status'] = true;
            return response()->json($rs);
        }       
        $rs['msg'] = $flag['msg'];
        return response()->json($rs);

    }

    /**
     * 审核用户提出的取消申请
     */
     public function approval(OrderRequest $request)
    {
        $rs['status'] = false;
        $data['id']           = $request->order_id;
        $data['shop_id']      = $request->shopUser->shop_id;
        $data['shop_user_id'] = $request->shopUser->id;
        $data['status']       = $request->status;
        $flag = OrderRepo::approval($data);
         if($flag['status']==true) {
            $rs['status'] = true;
            return response()->json($rs);
        }
       
        $rs['msg'] = $flag['msg'];
        return response()->json($rs);

    }

    /**
     * 配送订单需要送货 需要上传商品照片
     */
    public function uploadPic(OrderRequest $request)
    {
        $validatedData = $request->validate([
            'pic' => 'required',        
        ]);


        $rs['status'] = false;
        $data = $request->pure();
        
        $data['shop_user_id'] = $request->shopUser->id;       
        $flag = OrderRepo::orderPic($data);

        if($flag['status'] == true) {
            $rs['status'] = true;
            return response()->json($rs);
        }
        //dd($flag);
        $rs['msg'] = $flag['msg'];
        return response()->json($rs);
    }

    /**
     * 配送订单需要送货 
     */
    public function dispatchProduct(OrderRequest $request)
    {
        $rs['status']         = false;
        $data                 = $request->pure();
        $data['shop_user_id'] = $request->shopUser->id;
        $flag = OrderRepo::dispatch($data);
        if($flag['status']==true) {
            $rs['status'] = true;
            return response()->json($rs);
        }
        $rs['msg'] = $flag['msg'];
        return response()->json($rs);
    }

    /**
     * 商家扫描用户的订单二维码，这一步是在用户扫描商家付款码之后
     * 相当于确定 商品送达并已经付款。
     */
    public function validPay(OrderRequest $request)
    {
    	$rs['status']         = false;
        $data                 = $request->pure();
        $data['shop_user_id'] = $request->shopUser->id;
        $data['shop_id']      = $request->shopUser->shop_id;
        $flag = OrderRepo::validPay($data);
        if($flag['status']==true) {
            $rs['status'] = true;
            return response()->json($rs);
        }
        $rs['msg'] = $flag['msg'];
        return response()->json($rs);
    }


    public function total(OrderRequest $request)
    {
        $rs['status'] = true;
        $where['shop_id'] = $request->shopUser->shop_id;
        $rs['data'] = OrderRepo::total($where);
       
        return response()->json($rs);
        
        
    }
}
